package com.rongc.client.freight.business.carrier.view.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AbsListView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.exception.RegexException;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.RegexUtil;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.view.widget.DividerItemDecoration;
import com.rongc.client.freight.business.carrier.view.adapter.RecyclerChoiceConveyanceAdapter;
import com.rongc.client.freight.business.mine.model.CarBean;
import com.rongc.client.freight.utils.UtilMethod;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by ccqx on 2017/7/7.
 */

public class ConveyanceCarActivity extends BaseActivity {
    @Bind(R.id.recycler)
    RecyclerView mRecyclerView;
    RecyclerChoiceConveyanceAdapter modelAdapter;

    List<CarBean> datas;
    CarBean result;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.map_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_define:
                try{

                    SparseBooleanArray sp = modelAdapter.getSp();
                    for (int i = 0; i < sp.size(); i++) {
                        int key = sp.keyAt(i);
                        if (sp.get(key))
                            if(modelAdapter.getDatas().size() > key)
                                result = modelAdapter.getDatas().get(key);
                    }
                    prepare();
                    Intent intent = new Intent();
                    intent.putExtra("data",result);
                    setResult(RESULT_OK,intent);
                    finish();
                }catch (RegexException e){
                    ActivityUtils.toast(e.getMessage());
                }


                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    void prepare() throws RegexException {
        if(!RegexUtil.checkNotNull(result)){
            throw new RegexException("请选择车辆");
        }
    }

    @Override
    public void initView() {
        initToolbar(R.string.conveyance_create_car);

        datas = new ArrayList<>();

        modelAdapter = new RecyclerChoiceConveyanceAdapter(this,datas);
        modelAdapter.setChoiseMode(AbsListView.CHOICE_MODE_SINGLE);
        datas.add(new CarBean("3.1米 1吨 8方 / 箱式车"));
        datas.add(new CarBean("5.2米 5吨 25方 / 箱式车"));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setAdapter(modelAdapter);


    }

    Response.Listener<CarBean[]> respWorkersListener = new Response.Listener<CarBean[]>() {

        @Override
        public void onResponse(CarBean response[]) {
            if (HandleCode.requestSuccess()) {
                if(response!= null && response.length >0) {
                    datas.clear();
                    for (int i = 0; i < response.length; i++) {
                        datas.add(response[i]);
                    }
                }
            } else {
                UtilMethod.dismissProgressDialog(ConveyanceCarActivity.this);
            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            UtilMethod.dismissProgressDialog(ConveyanceCarActivity.this);
        }
    };

    @Override
    public void initData() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.universal_recycler;
    }
}
